package com.cqhilink.iot.jobstandardmanagement.services.innerservice.button.util;

import com.cqhilink.iot.jobstandardmanagement.common.bean.ApplicationContextHelper;
import com.cqhilink.iot.jobstandardmanagement.facade.model.RoleMenuButton;
import com.cqhilink.iot.jobstandardmanagement.facade.webservicesmodel.UasRoleRes;
import com.cqhilink.iot.jobstandardmanagement.facade.service.permission.JsmRoleMenuButtonService;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/2/17.
 */
public class RoleMenuButtonPool implements Runnable {
    private List<UasRoleRes> list;
    private String controlType;//1.添加，2.修改，3.删除

    public void setControlType(String controlType) {
        this.controlType = controlType;
    }

    public void setList(List<UasRoleRes> list) {
        this.list = list;
    }

    @Override
    public void run() {
        JsmRoleMenuButtonService roleMenuButtonService = (JsmRoleMenuButtonService) ApplicationContextHelper.getBean("jsmRoleMenuButtonService");
        if (roleMenuButtonService != null && StringUtils.isNotEmpty(controlType)) {
            List<RoleMenuButton> paramList = new ArrayList<RoleMenuButton>();
            try {
                roleMenuButton(list, paramList);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (paramList.size() > 0) {//角色权限处理
                if ("1".equals(controlType)) {
                    //添加
                    roleMenuButtonService.insertList(paramList);
                } else if ("2".equals(controlType)) {
                    //修改
                    roleMenuButtonService.updateList(paramList);
                } else if ("3".equals(controlType)) {
                    //删除
                    roleMenuButtonService.deleteList(paramList);
                }
            }
        }
    }

    /**
     * 处理角色权限信息
     *
     * @param list
     * @param paramList
     * @throws Exception
     */
    private void roleMenuButton(List<UasRoleRes> list, List<RoleMenuButton> paramList) throws Exception {
        if (list != null && list.size() > 0) {
            for (UasRoleRes urr : list) {
                RoleMenuButton param = new RoleMenuButton();
                BeanUtils.copyProperties(param, urr);
                param.setRoleMenuButtonId(urr.getRoleResId());
                param.setState(0);
                param.setButtonId(urr.getFuncId());
                paramList.add(param);
            }
        }
    }
}
